package main

import (
	"fmt"
	"net/http"
	"net/http/httputil"
)

func getDemo() {
	resp, err := http.Get("http://www.imooc.com")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	s, err := httputil.DumpResponse(resp, true)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s", s)
}

func DiyRequest() {
	req, err := http.NewRequest(
		http.MethodGet,
		"http://www.imooc.com",
		nil)
	req.Header.Add("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36 Edg/114.0.1823.67")
	if err != nil {
		panic(err)
	}
	client := http.Client{
		CheckRedirect: func(req *http.Request, via []*http.Request) error {
			fmt.Println("Redirect: ", req)
			return nil
		},
	}
	resp, err := client.Do(req)
	// resp, err := http.DefaultClient.Do(req)
	//if err != nil {
	//	panic(err)
	//}
	defer resp.Body.Close()
	s, err := httputil.DumpResponse(resp, true)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s", s)
}

func main() {
	DiyRequest()
}
